#https://leetcode.cn/leetbook/read/array-and-string/yf47s/
#2024/10/11
class Solution:
    def pivotIndex(self,nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        total_sum=sum(nums)
        leftSum=0
        for i in range(len(nums)):
            right_sum=total_sum-nums[i]-leftSum
            if right_sum==leftSum:
                return i
            leftSum=leftSum+nums[i]
        
        return -1

#2024/11/14
    def pivotIndex1(self, nums):
        leftSum=0
        sum=0
        for i in range(len(nums)):
            sum=sum+nums[i]
        rightSum=0
        for i in range(len(nums)):
            rightSum=sum-nums[i]-leftSum
            if leftSum==rightSum:
                return i
            leftSum=nums[i]+leftSum
            
        return -1


nums = [1, 7, 3, 6, 5, 6]
nums1 = [1, 2, 3]
nums2 = [2, 1, -1]
solution=Solution()
print(solution.pivotIndex(nums))
print(solution.pivotIndex(nums1))
print(solution.pivotIndex(nums2))
print(solution.pivotIndex1(nums))
print(solution.pivotIndex1(nums1))
print(solution.pivotIndex1(nums2))

